package com.unknow.first.dapp.admin.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.unknow.first.dapp.admin.account.dto.AddressSwapDTO;
import com.unknow.first.dapp.admin.account.dto.AddressSwapEstimateDTO;
import com.unknow.first.dapp.admin.account.mapper.FlashSwapRecord;
import com.unknow.first.dapp.admin.account.service.FlashSwapRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant.AuthMethod;
import org.cloud.context.RequestContextManager;
import org.cloud.vo.CommonApiResult;
import org.common.CommonPage;
import org.common.CommonParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flash/swap/record/app")
@Api(value = "APP:闪兑交易API", tags = "APP:闪兑交易API")
public class FlashSwapRecordController {


    @Autowired
    private FlashSwapRecordService flashSwapRecordService;


    @GetMapping
    @ApiOperation("查询兑换记录")
    @SystemResource(value = "/list", description = "查询兑换记录", authMethod = AuthMethod.ALLSYSTEMUSER)
    public CommonApiResult<CommonPage<FlashSwapRecord>> list(CommonParam commonParam) {

        LambdaQueryWrapper<FlashSwapRecord> queryWrapper = Wrappers.<FlashSwapRecord>lambdaQuery()
            .eq(FlashSwapRecord::getAddress, RequestContextManager.single().getRequestContext().getUser().getUsername());

        PageHelper.startPage(commonParam.getPage(), commonParam.getLimit(), commonParam.getSorts());
        return CommonApiResult.createSuccessResult(CommonPage.restPage(flashSwapRecordService.list(queryWrapper)));
    }

    @PostMapping
    @ApiOperation("兑换")
    @SystemResource(value = "/swap", description = "兑换", authMethod = AuthMethod.ALLSYSTEMUSER)
    public CommonApiResult<FlashSwapRecord> swap(@RequestBody AddressSwapDTO createDTO) throws Exception {
        FlashSwapRecord flashSwapRecord = flashSwapRecordService.create(createDTO, RequestContextManager.single().getRequestContext().getUser().getUsername());
        return CommonApiResult.createSuccessResult(flashSwapRecord);
    }

    @PostMapping("/estimate")
    @ApiOperation("预估")
    @SystemResource(value = "/estimate", description = "预估", authMethod = AuthMethod.ALLSYSTEMUSER)
    public CommonApiResult<AddressSwapEstimateDTO> estimate(@RequestBody AddressSwapDTO createDTO) throws Exception {
        return CommonApiResult.createSuccessResult(flashSwapRecordService.estimate(createDTO, RequestContextManager.single().getRequestContext().getUser().getUsername()));
    }

}
